"""
侧边栏演示程序
展示如何使用 Sidebar 组件
"""
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QLabel
from PySide6.QtCore import Qt
from PySide6.QtGui import QIcon
from sidebar import Sidebar


class MainWindow(QMainWindow):
    """主窗口"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("侧边栏演示")
        self.resize(800, 600)
        
        # 中心部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        main_layout = QHBoxLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 创建侧边栏
        self.sidebar = Sidebar()
        
        # 添加"文件"分组
        file_section = self.sidebar.add_section("文件")
        btn_new = file_section.add_button("新建")
        btn_open = file_section.add_button("打开")
        btn_save = file_section.add_button("保存")
        file_section.add_separator()
        btn_exit = file_section.add_button("退出")
        
        # 添加"编辑"分组
        edit_section = self.sidebar.add_section("编辑")
        btn_copy = edit_section.add_button("复制")
        btn_paste = edit_section.add_button("粘贴")
        btn_cut = edit_section.add_button("剪切")
        
        # 添加"视图"分组
        view_section = self.sidebar.add_section("视图")
        btn_zoom_in = view_section.add_button("放大")
        btn_zoom_out = view_section.add_button("缩小")
        btn_fullscreen = view_section.add_button("全屏")
        
        # 连接按钮事件
        self.sidebar.connect_button(btn_new, lambda: self.on_action("新建"))
        self.sidebar.connect_button(btn_open, lambda: self.on_action("打开"))
        self.sidebar.connect_button(btn_save, lambda: self.on_action("保存"))
        self.sidebar.connect_button(btn_exit, lambda: self.close())
        
        self.sidebar.connect_button(btn_copy, lambda: self.on_action("复制"))
        self.sidebar.connect_button(btn_paste, lambda: self.on_action("粘贴"))
        self.sidebar.connect_button(btn_cut, lambda: self.on_action("剪切"))
        
        self.sidebar.connect_button(btn_zoom_in, lambda: self.on_action("放大"))
        self.sidebar.connect_button(btn_zoom_out, lambda: self.on_action("缩小"))
        self.sidebar.connect_button(btn_fullscreen, lambda: self.on_action("全屏"))
        
        # 连接信号
        self.sidebar.button_clicked.connect(self.on_sidebar_button_clicked)
        
        # 内容区域
        self.content_label = QLabel("点击侧边栏按钮查看效果")
        self.content_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.content_label.setStyleSheet("""
            QLabel {
                font-size: 24px;
                color: #666;
            }
        """)
        
        # 布局组装
        main_layout.addWidget(self.sidebar)
        main_layout.addWidget(self.content_label, 1)
    
    def on_action(self, action_name):
        """处理按钮点击"""
        self.content_label.setText(f"执行操作: {action_name}")
        print(f"✅ 执行操作: {action_name}")
    
    def on_sidebar_button_clicked(self, button_text):
        """侧边栏按钮点击信号处理"""
        print(f"📌 侧边栏信号: {button_text}")


def main():
    app = QApplication(sys.argv)
    
    # 设置应用样式
    app.setStyle("Fusion")
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec())


if __name__ == "__main__":
    main()
